#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

const ll N = 3e5 + 10, INF = 0x3f3f3f3f;

ll n, M;
ll a[N];
ll C[5010][5010];

void solve()
{
    ll s = 0;
    cin >> n;
    for (ll i = 1;i <= n;i++) { cin >> a[i];s += a[i]; }

    ll ans = 1;
    for (ll i = 1;i <= n;i++) {
        (ans *= C[s][a[i]]) %= M;
        s -= a[i];
    }
    cout << ans << '\n';
}

signed main()
{
    //ios::sync_with_stdio(0);
    //cin.tie(0);cout.tie(0);
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    ll T = 1;
    cin >> T >> M;
    C[0][0] = C[1][0] = C[1][1] = 1;
    for (ll i = 2;i <= 5000;i++) {
        C[i][0] = 1;
        for (ll j = 1;j <= i;j++)
            C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % M;
    }
    for (ll i = 1;i <= T;i++) {
        solve();
    }
}